package com.gfm.story.service.story;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gfm.story.dao.*;
import com.gfm.story.entity.dict.Dictionary;
import com.gfm.story.entity.story.Book;
import com.gfm.story.entity.story.Category;
import com.gfm.story.entity.story.Chapter;
import com.gfm.story.entity.story.Store;
import com.gfm.story.entity.user.User;
import com.gfm.story.interceptor.UserContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @BelongsProject: story
 * @BelongsPackage: com.gfm.story.service.story
 * @Author: Administrator
 * @CreateTime: 2019-06-04 14:43
 * @Description: ${Description}
 */
@Service
public class StoreService {
    @Resource
    private StoreDao storeDao;
    @Resource
    private DictionaryDao dictionaryDao;
    @Resource
    private ChapterService chapterService;
    @Resource
    private CategoryDao categoryDao;
    @Resource
    private BookDao bookDao;

    /**
     * 根据用户id查询收藏书签
     * 1.小说名/最新章节/书签/更新时间/
     * @param
     * @return
     */
    public Map<String, Object> getAllStore(Long userId,String nameOrAuthor) {
        com.gfm.story.entity.dict.Dictionary dictionary = new Dictionary();
        dictionary.setDictCode("BookDomainAddress");
        dictionary = dictionaryDao.selectOne(dictionary);
        //1.根据用户id获取收藏关联表(得到书签id(章节))
        List<Store> storeList = storeDao.selectStoreByUserId(userId);
        //2.根据书签id获取小说信息
        Map<String,Object> map = new HashMap<String, Object>();
        List<Store> list = new ArrayList<Store>();
        if (storeList!=null){
            Book book = new Book();
            for (int i=0;i<storeList.size();i++){
                Store store1 = new Store();
                Chapter chapter = new Chapter();
                book = bookDao.selectBookById(storeList.get(i).getBookId(),null);
                //查询最新章节
                Chapter newChapter = new Chapter();
                if (book!=null){
                    newChapter = chapterService.getChapterByBookId(book.getId());
                    if (storeList.get(i)!=null){//查询已读到的章节
                        List<Chapter> chapters = chapterDao.getChapterByChapterId(storeList.get(i).getChapterId());
                        if (!chapters.isEmpty()){
                            chapter = chapters.get(0);
                        }
                    }
                }
                store1.setId(storeList.get(i).getId());
                store1.setUserId(userId);
                store1.setBookAuthor(book.getAuthor());
                store1.setBookId(book.getId());
                store1.setImgUrl(dictionary.getDisc()+book.getImgUrl());
                store1.setStoryName(book.getName());//小说名
                store1.setChapterId(chapter.getId());
                store1.setStoreName(chapter.getTitle());//书签
                if (newChapter==null){
                    store1.setNewChapterId(null);
                    store1.setChapterTime(null);
                    store1.setNewChapter("");
                }else{
                    store1.setNewChapterId(newChapter.getId());
                    store1.setChapterTime(newChapter.getUpdateTime());//最新章节更新时间
                    store1.setNewChapter(newChapter.getTitle());//最新章节名称
                }
                if (nameOrAuthor!=null){
                    if (store1.getStoryName().indexOf(nameOrAuthor)>=0 || store1.getBookAuthor().indexOf(nameOrAuthor)>=0){
                        list.add(store1);
                    }
                }else{
                    list.add(store1);
                }
            }
        }
        map.put("StoreList", list);
        map.put("nameOrAuthor", nameOrAuthor);
        return map;

    }

    public boolean remove(Long storeId) {
        Store store = new Store();
        if (storeId!=null){
            store.setId(storeId);
            if (store.deleteById()){
                return true;
            }

        }
    return false;
    }

    @Resource
    private ChapterDao chapterDao;

    public boolean addOrEdit(Long bookId,Long userId, Long chapterId) {

        //在小说主页直接添加书签
        if (chapterId==null){
            Store store = new Store();
            store.setBookId(bookId);
            store.setUserId(userId);
            Store store1 = storeDao.getStoreByUserOrBook(bookId,userId);
            //根据小说id获取章节
            List<Chapter> chapters = chapterDao.getChapterByBookId(bookId);
            if (!chapters.isEmpty()){
                store.setChapterId(chapters.get(0).getId());
            }else {
                store.setChapterId(Long.parseLong("0"));
            }
            if (store1!=null){//不存在同样的书签则插入
                if (store1.getBookId()==bookId){//同一本书下有重复的书签则删除旧书签添加新书签
                    store1.deleteById();
                    //根据用户
                    store.insert();//插入新书签
                }
                //book收藏记录+1
                Book book = new Book();
                book.setId(bookId);
                Book book1 = bookDao.selectBookById(bookId,null);
                book.setCollectionCount((book1.getCollectionCount()==null?0:book1.getCollectionCount())+1);
                book.updateById();
                return true;
            }else {
                store.insert();//插入新书签
                Book book = new Book();
                book.setId(bookId);
                Book book1 = bookDao.selectBookById(bookId,null);
                book.setCollectionCount((book1.getCollectionCount()==null?0:book1.getCollectionCount())+1);
                book.updateById();
                return true;
            }
        }else{//在章节处添加书签
            Store store = new Store();
            store.setBookId(bookId);
            store.setUserId(userId);
            Store store1 = storeDao.getStoreByChapterOrUserOrBook(bookId,userId);
            if (store1!=null){//不存在同样的书签则插入
                System.out.println(store1.getBookId());
                System.out.println(bookId);
                if (store1.getBookId()==bookId || store1.getBookId().equals(bookId)){//同一本书下有重复的书签则删除旧书签添加新书签
                    store1.deleteById();
                    //根据用户
                    store.setChapterId(chapterId);
                    store.insert();//插入新书签
                }
                //book收藏记录+1
                Book book = new Book();
                book.setId(bookId);
                Book book1 = bookDao.selectBookById(bookId,null);
                book.setCollectionCount((book1.getCollectionCount()==null?0:book1.getCollectionCount())+1);
                book.updateById();
                return true;
            }else{
                store.setChapterId(chapterId);
                store.insert();//插入新书签
                Book book = new Book();
                book.setId(bookId);
                Book book1 = bookDao.selectBookById(bookId,null);
                book.setCollectionCount((book1.getCollectionCount()==null?0:book1.getCollectionCount())+1);
                book.updateById();
                return true;
            }
        }
    }

    //收藏总榜
    public Map<String, Object> getTotalPage(Map<String, Object> map,Integer page) {
        List<Book> list = bookDao.getStoryPage(map);
        for (Book book:list){
            if (book.getCategoryId()!=null){
                //修改图书类型
                Category category = new Category();
                category.setId(book.getCategoryId());
                Category category1 = categoryDao.selectOne(category);
                book.setCategoryName(category1.getName());
            }else{
                book.setCategoryName("其他小说");
            }
        }
        int count =bookDao.getStoryPageCount(map);
        Map<String,Object> map1 = new HashMap<String, Object>();
        map1.put("objs",list);
        map1.put("total", (count+20-1)/20);
        map1.put("currentPage", page);
        return map1;
    }
}
